﻿using System;
using CoreTypes;
using System.Custom;
using System.Drawing;
using System.Custom.Window;
using System.Globalization;
using System.Windows.Forms;
using MainModule.Properties;
using System.ComponentModel;
using System.Drawing.Imaging;

namespace MainModule
{
   public partial class UserProperties : Window
   {
      #region Window Data

      User FUser;
      Account FOwner;
      Font FFontB, FFontU;
      bool FAssigningPrivacy;
      ContactSettings FSettings;
      static int FCurrentTextFormatIndex;
      static ContextMenuStrip FMenuZoomedPic;      
      InternetCommunicator FCommunicator = InternetCommunicator.Self;

      #endregion

      static UserProperties()
      {
         FMenuZoomedPic = new ContextMenuStrip();

         FMenuZoomedPic.AutoSize = false;
         FMenuZoomedPic.ShowImageMargin = false;
         FMenuZoomedPic.ShowCheckMargin = false;
         FMenuZoomedPic.ShowItemToolTips = true;

         var item = FMenuZoomedPic.Items.Add("");

         item.ToolTipText = Resources.S_Click_to_save_picture;
         item.AutoSize = false;
         item.Padding = new Padding(1, 0, 1, 1);
         item.Margin = item.Padding;

         FMenuZoomedPic.MouseLeave += (sender, e) =>
         {
            FMenuZoomedPic.Close();
         };
         FMenuZoomedPic.Opening += (sender, e) =>
         {
            Image pic = (Image)FMenuZoomedPic.Tag;

            item.Size = pic.Size;
            FMenuZoomedPic.Width = pic.Width + 3;
            FMenuZoomedPic.Height = pic.Height + 4;

            MenuWrapper.MakeFadeEffect(FMenuZoomedPic, 0.95);
         };
         item.Paint += (sender, e) =>
         {
            Image pic = (Image)FMenuZoomedPic.Tag;
            if (pic != null) e.Graphics.DrawImageUnscaled(pic, 1, 0);
         };
         item.Click += (sender, e) =>
         {
            Image pic = (Image)FMenuZoomedPic.Tag;
            SaveUserpic(pic, pic.Tag.ToString());
         };
      }
      public UserProperties()
      {
         InitializeComponent();

         FCommunicator.PropertyChanged += communicator_PropertyChanged;
         FCommunicator.RequestUserInfoState += communicator_RequestUserInfoState;

         ColorTheme = FCommunicator.ColorTheme;
         RenderMode = FCommunicator.RenderMode;

         dataTablePolicyValues.Rows.Add(PolicyRule.None, Resources.S_Use_account_settings);
         dataTablePolicyValues.Rows.Add(PolicyRule.Allow, Resources.S_Allow);
         dataTablePolicyValues.Rows.Add(PolicyRule.Ask, Resources.S_Ask_me);
         dataTablePolicyValues.Rows.Add(PolicyRule.Block, Resources.S_Block);
         dataTablePolicyValues.Rows.Add(PolicyRule.Ignore, Resources.S_Ignore);

         FFontB = new Font(richTextBoxSummary.Font, FontStyle.Bold);
         FFontU = new Font(richTextBoxSummary.Font, FontStyle.Underline);

         window_AeroGlassEffectChanged(null, null);
      }

      protected override void Dispose(bool disposing)
      {
         FCommunicator.PropertyChanged -= communicator_PropertyChanged;
         FCommunicator.RequestUserInfoState -= communicator_RequestUserInfoState;

         if (disposing && (components != null))
         {
            components.Dispose();
         }
         base.Dispose(disposing);
      }

      public override ColorTheme ColorTheme
      {
         get
         {
            return base.ColorTheme;
         }
         set
         {
            base.ColorTheme = value;
            Refresh();
         }
      }

      public static void HideZoom()
      {
         FMenuZoomedPic.Close();
      }
      public void Show(User user, string page)
      {
         AssignUser(user);

         string action = page.SubstrAfter("/");
         page = page.SubstrBefore("/");

         tabControl.SelectedTab = tabControl.TabPages.ContainsKey("tabPage" + page) ?
                                    tabControl.TabPages["tabPage" + page] :
                                    tabControl.TabPages["tabPageSummary"];

         buttonPrivacyDetails_Click(this, null);
         tabControl_SelectedIndexChanged(this, null);

         checkBoxBlockContact.Text = checkBoxBlockContact.Text.Replace("<user>", "\"" + FUser.CustomName + "\"");
         checkBoxVisibilityRuleAllow.Text = checkBoxVisibilityRuleAllow.Text.Replace("<user>", "\"" + FUser.CustomName + "\"");
         checkBoxVisibilityRuleBlock.Text = checkBoxVisibilityRuleBlock.Text.Replace("<user>", "\"" + FUser.CustomName + "\"");

         checkBoxVisibilityRuleAllow.Enabled = FOwner.ServiceConfig.SupportsFeature(ServiceFeatures.AllowList);
         checkBoxVisibilityRuleBlock.Enabled = FOwner.ServiceConfig.SupportsFeature(ServiceFeatures.BlockList);

         checkBoxHideUser.Text = checkBoxHideUser.Text.Replace("<user>", "\"" + FUser.CustomName + "\"");

         Size = MinimumSize;
         CenterToScreen();
         Visualize();

         if (action == "CustomName")
         {
            textBoxCustomName.Focus();
            textBoxCustomName.SelectAll();
         }
         else
         {
            buttonClose.Focus();
         }
      }
      public static void ZoomUserpic(Form parent, User user)
      {
         Image oldPic = (Image)FMenuZoomedPic.Tag;
         if (oldPic != null) oldPic.Dispose();
         
         if (!user.Info.HasUserpic) return;

         Image newPic = user.Info.Userpic;

         newPic.Tag = user.Login;
         FMenuZoomedPic.Tag = newPic;

         int x = parent.Left - newPic.Width - 10;

         MenuWrapper.ShowMenu(FMenuZoomedPic, parent.Left + (x >= 0 ? -newPic.Width - 10 : parent.Width + 10), parent.Top + 50);
      }
      public static void ComposeSummary(User user, RichTextBox richTextBox, Font catFont, Font itemFont)
      {
         richTextBox.Visible = false;

         try
         {
            richTextBox.Clear();

            AddSummaryCategory(richTextBox, Resources.S_General);
            AddSummaryItem(richTextBox, Resources.S_Nickname, user.Nickname, "-", "\r\n");
            AddSummaryItem(richTextBox, Resources.S_Full_name, user.Info.FullName, "-", "\r\n");
            if (user.Info.Language != "") AddSummaryItem(richTextBox, Resources.S_Language, user.Info.Language, "-", "\r\n");
            if (user.Info.Gender.HasValue) AddSummaryItem(richTextBox, Resources.S_Gender, UserInfo.GetGenderAsString(user.Info.Gender, true), "-", "\r\n");

            string bd = user.Info.GetBirthdayAsString(true);
            if (bd != "") AddSummaryItem(richTextBox, Resources.S_Birthday, bd, "-", "\r\n");
            string age = user.Info.Age;
            if (age != "") AddSummaryItem(richTextBox, Resources.S_Age, age, "-", "\r\n");

            richTextBox.Text += "\r\n";

            Contact contact = user as Contact;
            Account account = user as Account;

            string loginStr = contact != null ? contact.ServiceConfig.Service.LoginAlias :
                             (account != null ? account.ServiceConfig.Service.LoginAlias : user.LoginPrefix);

            AddSummaryCategory(richTextBox, Resources.S_Contact);
            AddSummaryItem(richTextBox, loginStr, user.Login, "-", "\r\n");
            AddSummaryItem(richTextBox, Resources.S_Email, user.Info.Email, "-", "\r\n");
            AddSummaryItem(richTextBox, Resources.S_Phone, user.Info.Phone, "-", "\r\n");
            if (user.Info.PersonalUrl != "") AddSummaryItem(richTextBox, Resources.S_Personal_url, user.Info.PersonalUrl, "-", "\r\n");

            AddSummaryCategory(richTextBox, Resources.S_Location);
            AddSummaryItem(richTextBox, Resources.S_Country, user.Info.Country, "-", "\r\n");
            AddSummaryItem(richTextBox, Resources.S_City, user.Info.City, "-", "\r\n");

            if (user.Info.Work.IsPresent)
            {
               AddSummaryCategory(richTextBox, Resources.S_Work);
               if (user.Info.Work.Organization != "") AddSummaryItem(richTextBox, Resources.S_Organization, user.Info.Work.Organization, "-", "\r\n");
               if (user.Info.Work.Department != "") AddSummaryItem(richTextBox, Resources.S_Department, user.Info.Work.Department, "-", "\r\n");
               if (user.Info.Work.Role != "") AddSummaryItem(richTextBox, Resources.S_Role, user.Info.Work.Role, "-", "\r\n");
               if (user.Info.Work.Email != "") AddSummaryItem(richTextBox, Resources.S_Email, user.Info.Work.Email, "-", "\r\n");
               if (user.Info.Work.Phone != "") AddSummaryItem(richTextBox, Resources.S_Phone, user.Info.Work.Phone, "-", "\r\n");
               if (user.Info.Work.Url != "") AddSummaryItem(richTextBox, Resources.S_Web_site, user.Info.Work.Url, "-", "\r\n");
               if (user.Info.Work.Country != "" || user.Info.Work.City != "")
               {
                  AddSummaryItem(richTextBox, Resources.S_Location, user.Info.Work.Country + " " + user.Info.Work.City, "-", "\r\n");
               }
            }

            if (user.Info.Description != "")
            {
               AddSummaryCategory(richTextBox, Resources.S_Misc);
               AddSummaryItem(richTextBox, Resources.S_Description, user.Info.Description, "", "\r\n");
            }

            if (contact != null)
            {
               AddSummaryCategory(richTextBox, Resources.S_Other_information);
               AddSummaryItem(richTextBox, Resources.S_Status_message, contact.Status.ToLongString(), "", "\r\n");
            }
            else if (account != null)
            {
               if (!string.IsNullOrEmpty(account.StatusMessage))
               {
                  AddSummaryCategory(richTextBox, Resources.S_Other_information);
                  AddSummaryItem(richTextBox, Resources.S_Status_message, account.StatusMessage, "", "\r\n");
               }
            }

            FCurrentTextFormatIndex = 0;

            FormatSummaryItem(richTextBox, catFont, Resources.S_General + ":", false, true);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Nickname + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Full_name + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Language + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Gender + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Birthday + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Age + ":", false, false);

            FormatSummaryItem(richTextBox, catFont, Resources.S_Contact + ":", false, true);
            FormatSummaryItem(richTextBox, itemFont, loginStr + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Email + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Phone + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Personal_url + ":", false, false);

            FormatSummaryItem(richTextBox, catFont, Resources.S_Location + ":", false, true);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Country + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_City + ":", false, false);

            FormatSummaryItem(richTextBox, catFont, Resources.S_Work + ":", false, true);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Organization + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Department + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Role + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Email + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Phone + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Web_site + ":", false, false);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Location + ":", false, false);

            FormatSummaryItem(richTextBox, catFont, Resources.S_Misc + ":", false, true);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Description + ":", false, false);

            FormatSummaryItem(richTextBox, catFont, Resources.S_Other_information + ":", false, true);
            FormatSummaryItem(richTextBox, itemFont, Resources.S_Status_message + ":", false, false);
         }
         finally
         {
            richTextBox.Visible = true;
         }
      }
  
      private static void SaveUserpic(Image image, string fileName)
      {
         SaveFileDialog sd = new SaveFileDialog();
         sd.FileName = fileName + ".png";
         sd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
         if (sd.ShowDialog() == DialogResult.OK)
         {
            image.Save(sd.FileName, ImageFormat.Png);
         }
         sd.Dispose();
      }
      private static void AddSummaryCategory(RichTextBox textBox, string caption)
      {
         if (textBox.Text != "") textBox.Text += "\r\n";
         textBox.Text += caption + ":\r\n";
      }
      private static void AddSummaryItem(RichTextBox textBox, string caption, string value, string empty, string endl)
      {
         textBox.Text += caption + ":    " + (string.IsNullOrEmpty(value.TrimIgnoreNull()) ? empty : value.Replace("&quot;", "\"")) + endl;
      }
      private static void FormatSummaryItem(RichTextBox textBox, Font font, string item, bool center, bool category)
      {
         int start = textBox.Text.IndexOf(item, FCurrentTextFormatIndex);

         if (start < 0) return;

         FCurrentTextFormatIndex = start + item.Length;

         textBox.Select(start, item.Length);
         textBox.SelectionFont = font;
         if (center) textBox.SelectionAlignment = HorizontalAlignment.Center;
         if (!category && !center) textBox.SelectionIndent = 45;
         textBox.Select(0, 0);
      }

      private bool Save()
      {
         if (FUser is Contact)
         {
            Contact contact = FUser as Contact;

            FCommunicator.SetPermissions(contact.Owner, contact, FSettings.MessageRule, FSettings.InvitationRule, FSettings.VisibilityRule);

            if (contact.CustomName != textBoxCustomName.Text)
            {
               contact.Owner.ContactsVersion++;
               contact.CustomName = textBoxCustomName.Text;
               FCommunicator.SaveAccountData(contact.Owner, SaveAccountActions.RenameContactsItem, contact);
            }
            else if (contact.Visible != !checkBoxHideUser.Checked)
            {
               contact.Visible = !checkBoxHideUser.Checked;
               FCommunicator.SaveAccountData(contact.Owner, SaveAccountActions.ModifyContact, contact);
            }
            FCommunicator.SaveContacts();

            return true;
         }

         return false;
      }
      private void SetupUserpic()
      {
         buttonZoom.Visible = false;
         buttonSaveUserpic.Visible = true;

         Image pic = FUser.Info.Userpic;

         try
         {
            if (pic == null)
            {
               buttonSaveUserpic.Visible = false;
               pictureBox.Image = FCommunicator.DefaultUserpic;
               pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
            }
            else
            {
               pictureBox.Image = pic;

               if (pic.Width > pictureBox.Width || pic.Height > pictureBox.Height)
               {
                  buttonZoom.Visible = true;
                  pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
               }
               else if (pic.Width == pictureBox.Width && pic.Height == pictureBox.Height)
               {
                  pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
               }
               else
               {
                  pictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
               }
            }
         }
         catch (Exception error)
         {
            panelWarning.Tag = new Exception("Unable to show picture:", error);
            panelWarning.Visible = true;
         }
      }
      private void AssignPrivacy()
      {
         FAssigningPrivacy = true;

         try
         {
            comboBoxContactMessageRule.SelectedValue = FSettings.MessageRule;
            comboBoxContactInvitationRule.SelectedValue = FSettings.InvitationRule;

            checkBoxBlockContact.Checked = FSettings.MessageRule == PolicyRule.Block;
            checkBoxVisibilityRuleAllow.Checked = FOwner.ServiceConfig.SupportsFeature(ServiceFeatures.AllowList) ?
                                                  FSettings.VisibilityRule == PolicyRule.Allow : false;
            checkBoxVisibilityRuleBlock.Checked = FOwner.ServiceConfig.SupportsFeature(ServiceFeatures.BlockList) ?
                                                  FSettings.VisibilityRule == PolicyRule.Block : false;
         }
         finally
         {
            FAssigningPrivacy = false;
         }
      }
      private void AssignUser(User user)
      {
         FUser = user;
         Contact contact = FUser as Contact;

         if (contact == null)
         {
            buttonSave.Text = Resources.S_Add;
            FOwner = FCommunicator.CurrentAccount;
            tabControl.TabPages.Remove(tabPageContactOptions);
         }
         else
         {
            FOwner = contact.Owner;
            FSettings = contact.Settings;
            buttonSave.Text = Resources.S_Save;
            checkBoxHideUser.Checked = !contact.Visible;
            labelContactOwner.Text = FOwner.ToString();
            AssignPrivacy();
         }

         UpdatewWindowView();
         SetupUserpic();

         if (!FUser.Info.HasUserpic) FCommunicator.RequestUserInfo(FUser, false);
      }
      protected override void UpdatewWindowView()
      {
         textBoxCustomName.Text = FUser.CustomName;
         labelSummaryCaption.Text = FUser.CustomName;
         ComposeSummary(FUser, richTextBoxSummary, FFontU, FFontB);
         Text = Resources.S_Properties_of_user.Replace("<user>", FUser.CustomName);
      }

      private void buttonZoom_Click(object sender, EventArgs e)
      {
         UserProperties.ZoomUserpic(this, FUser);
      }
      private void buttonSave_Click(object sender, EventArgs e)
      {
         if (FUser is Contact)
         {
            Save();
         }
         else
         {
            FCommunicator.ShowAddContact(FUser, false);
         }
         Close();
      }
      private void buttonClose_Click(object sender, EventArgs e)
      {
         Close();
      }
      private void panelWarning_Click(object sender, EventArgs e)
      {
         panelWarning.Visible = false;

         if (panelWarning.Tag is Exception)
         {
            FCommunicator.ShowError(Resources.S_Request_info_error.Replace("<user>", FUser.CustomName), panelWarning.Tag as Exception);
         }

         panelWarning.Tag = null;
      }
      private void window_SizeChanged(object sender, EventArgs e)
      {
         tabControl.Refresh();
      }
      private void buttonRefresh_Click(object sender, EventArgs e)
      {
         FCommunicator.RequestUserInfo(FUser, false);
      }
      private void panelBottom_Paint(object sender, PaintEventArgs e)
      {
         if (!AeroGlassVisible) FCommunicator.DrawTheme(FCommunicator.ColorTheme, e.Graphics, (sender as Control).ClientRectangle, ColorThemeObject.PanelV);
      }
      private void pictureBox_MouseLeave(object sender, EventArgs e)
      {
         HideZoom();
      }
      private void buttonSaveUserpic_Click(object sender, EventArgs e)
      {
         SaveUserpic(pictureBox.Image, FUser.Login);
      }
      private void panelInfoBody_Paint(object sender, PaintEventArgs e)
      {
         FCommunicator.DrawTheme(ColorTheme.Standard, e.Graphics, (sender as Control).ClientRectangle, ColorThemeObject.Pattern);
         //FCommunicator.DrawTheme(ColorTheme, e.Graphics, (sender as Control).ClientRectangle, ColorThemeObject.Area);
      }
      private void panelInfoHeader_Paint(object sender, PaintEventArgs e)
      {
         FCommunicator.DrawTheme(ColorTheme.Standard, e.Graphics, (sender as Control).ClientRectangle, ColorThemeObject.Pattern);
         //FCommunicator.DrawTheme(ColorTheme, e.Graphics, (sender as Control).ClientRectangle, ColorThemeObject.Pattern);
      }
      private void tabPageContactOptions_Enter(object sender, EventArgs e)
      {
         textBoxCustomName.Focus();
      }
      private void window_AeroGlassEffectChanged(object sender, EventArgs e)
      {
         if (AeroGlassVisible)
         {
            buttonRefresh.Top = 10;
            buttonSave.Top = 10;
            buttonClose.Top = 10;
            panelWarning.Top = 12;
            buttonRefresh.Left = 4;
            buttonClose.Left = panelTab.Left + panelTab.Width - buttonClose.Width + 3;
         }
         else
         {
            buttonRefresh.Top = 7;
            buttonSave.Top = 7;
            buttonClose.Top = 7;
            panelWarning.Top = 9;
            buttonRefresh.Left = 6;
            buttonClose.Left = panelTab.Left + panelTab.Width - buttonClose.Width - 1;
         }
         buttonSave.Left = buttonClose.Left - buttonSave.Width - 6;
      }
      private void richTextBoxSummary_MouseUp(object sender, MouseEventArgs e)
      {
         if (e.Button == MouseButtons.Right) Window.ShowStandardTextMenu(richTextBoxSummary);
      }
      private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
      {
         if (tabControl.SelectedTab == tabPageSummary)
         {
            buttonSave.Visible = !(FUser is Contact);
         }
         else
         {
            buttonSave.Visible = true;
         }
      }
      private void richTextBoxSummary_LinkClicked(object sender, LinkClickedEventArgs e)
      {
         Shell.OpenLink(e.LinkText, "");
      }
      private void communicator_PropertyChanged(object sender, PropertyChangedEventArgs e)
      {
         if (e.PropertyName == "ColorTheme") ColorTheme = FCommunicator.ColorTheme;
      }
      private void communicator_RequestUserInfoState(object sender, NetworkOperationEventArgs e)
      {
         if (e.GetParam("User", null) != FUser) return;

         User user = (User)e.GetResult("User", null);

         if (e.Error != null)
         {
            panelWarning.Tag = e.Error;
            panelWarning.Visible = true;
            buttonRefresh.Enabled = true;
         }
         else if (e.State == OperationState.Completed)
         {
            if (user == null)
            {
               MessageBox.Show(Resources.S_User_does_not_exist.Replace("<id>", FUser.Identifier), Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
               UpdatewWindowView();
               SetupUserpic();
               buttonRefresh.Enabled = true;
            }
         }
         else
         {
            buttonRefresh.Enabled = false;
         }
      }

      private void labelContactOwner_Click(object sender, EventArgs e)
      {
         FCommunicator.CurrentAccount = FOwner;
      }
      private void buttonAuthorisation_Click(object sender, EventArgs e)
      {
         //((XmppServiceTransport.CommunicationServiceObject)(FUser as Contact).ServiceDriver).SetupSubscription(FUser as Contact, XmppServiceTransport.SubscriptionAction.Repair, true);
      }
      private void buttonPrivacyDetails_Click(object sender, EventArgs e)
      {
         int minH = panelContactPrivacy.MinimumSize.Height;

         //if (panelContactPrivacy.Height == minH) panelAuthorisation.Visible = false;

         labelPrivacyDetails.Text = panelContactPrivacy.Height > minH ?
                                        labelPrivacyDetails.Text.Replace("<<", ">>") :
                                        labelPrivacyDetails.Text.Replace(">>", "<<");
         panelContactPrivacy.Height = panelContactPrivacy.Height > minH ?  minH :
                                      panelContactPrivacy.MaximumSize.Height;

         //if (panelContactPrivacy.Height == minH) panelAuthorisation.Visible = true;

         panelContactPrivacy.Refresh();
      }
      private void checkBoxBlockContact_CheckedChanged(object sender, EventArgs e)
      {
         if (FAssigningPrivacy) return;

         FSettings.MessageRule = checkBoxBlockContact.Checked ? PolicyRule.Block : PolicyRule.None;
         FSettings.InvitationRule = checkBoxBlockContact.Checked ? PolicyRule.Block : PolicyRule.None;
         if (checkBoxBlockContact.Checked) FSettings.VisibilityRule = PolicyRule.Block;

         AssignPrivacy();
      }
      private void checkBoxVisibilityRuleAllow_CheckedChanged(object sender, EventArgs e)
      {
         if (FAssigningPrivacy) return;

         FSettings.VisibilityRule = checkBoxVisibilityRuleAllow.Checked ? PolicyRule.Allow : PolicyRule.None;
         FSettings.MessageRule = checkBoxVisibilityRuleAllow.Checked ? PolicyRule.Allow : PolicyRule.None;
         FSettings.InvitationRule = checkBoxVisibilityRuleAllow.Checked ? PolicyRule.Ask : PolicyRule.None;

         AssignPrivacy();
      }
      private void checkBoxVisibilityRuleBlock_CheckedChanged(object sender, EventArgs e)
      {
         if (FAssigningPrivacy) return;

         FSettings.VisibilityRule = checkBoxVisibilityRuleBlock.Checked ? PolicyRule.Block : PolicyRule.None;
         //FSettings.MessageRule = checkBoxVisibilityRuleBlock.Checked ? PolicyRule.Ask : PolicyRule.None;
         //FSettings.InvitationRule = checkBoxVisibilityRuleBlock.Checked ? PolicyRule.Block : PolicyRule.None;

         AssignPrivacy();
      }
      private void comboBoxContactMessageRule_SelectedIndexChanged(object sender, EventArgs e)
      {
         if (FAssigningPrivacy || comboBoxContactMessageRule.SelectedValue == null) return;

         FSettings.MessageRule = (PolicyRule)Enum.Parse(typeof(PolicyRule), (string)comboBoxContactMessageRule.SelectedValue);

         AssignPrivacy();
      }
      private void comboBoxContactInvitationRule_SelectedIndexChanged(object sender, EventArgs e)
      {
         if (FAssigningPrivacy || comboBoxContactInvitationRule.SelectedValue == null) return;

         FSettings.InvitationRule = (PolicyRule)Enum.Parse(typeof(PolicyRule), (string)comboBoxContactInvitationRule.SelectedValue);

         AssignPrivacy();
      }

      private void buttonSubscribe_Click(object sender, EventArgs e)
      {
         //((XmppServiceTransport.CommunicationServiceObject)(FUser as Contact).ServiceDriver).SetupSubscription(FUser as Contact, XmppServiceTransport.SubscriptionAction.Subscribe, true);
      }
      private void buttonUnsubscribe_Click(object sender, EventArgs e)
      {
         //((XmppServiceTransport.CommunicationServiceObject)(FUser as Contact).ServiceDriver).SetupSubscription(FUser as Contact, XmppServiceTransport.SubscriptionAction.Unsubscribe, true);
      }
   }
}